import { Component,Input, OnInit, Injector } from '@angular/core';
import {Dynamic} from 'pengesoft-ng-lib';
import { from } from 'rxjs';
import { FormBuilder, FormGroup, FormControl, Validators} from '@angular/forms';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
import { TreeNodeInterface } from '../../views/organize-manage/organize-manage.component';
import { DeptServiceSvr } from '../../services/dept.service'

@Component({
  selector: 'app-add-organize',
  templateUrl: './add-organize.component.html',
  styleUrls: ['./add-organize.component.less']
})
export class AddOrganizeComponent implements OnInit {
  form:FormGroup
  options: any = []
  nzOptions: NzCascaderOption[]
  values: string[] | null
  // 上级组织
  NodeId: number
  // 级联选择--所有组织
  getParentOrganize():any {
    this.DeptSvr.getAllMyDepartment().then(res => {
      this.options = [
        {
          value: res.NodeId,
          label: res.DeptName,
          children: res.ChildDepartment.map(items => {
            return {
              value: items.NodeId,
              label: items.DeptName,
              children: items.ChildDepartment.map(item => {
                return {
                  value: item.NodeId,
                  label: item.DeptName
                }
              })
            }
          })
        }
      ]
      // 级联选择
      this.nzOptions = this.options;
      this.values = null;
    })
  }

  constructor(
    public DeptSvr: DeptServiceSvr,
    public fb:FormBuilder,
    public modal:NzModalRef
  ) {}

  ngOnInit(): void {
    this.form = this.fb.group({
      Name: ['', Validators.required],
      ParentName: ['', Validators.required],
      Leader: ['']
    })
    this.getParentOrganize()
  }

  OnSubmit(event,form){
    this.DeptSvr.addDepartment(form.value.Name, this.NodeId, form.value.Leader).then(res => {
      // console.log("测试增加组织：",res)
    })
    this.modal.destroy({form})
  }

  // 取消，直接关闭
  cancel(){
    this.modal.destroy()
  }

  onChanges(values: string[]): void {
    if(values) {
      this.NodeId = Number(values[values.length-1]);
    }
  }

}
